package concurrent;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class FutureDemo {

    public static void main(String[] args) throws InterruptedException {
         ExecutorService executorService = Executors.newCachedThreadPool();
         java.util.concurrent.Future<String> future = executorService.submit(new Callable<String>() {
             @Override
             public String call() throws Exception {
                 Thread.sleep(2000);
                 System.out.println("run");
                 return "result";
             }
         });

         Thread.sleep(1000);
         future.cancel(true);
        try {
            System.out.println(future.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        executorService.shutdownNow();
    }
}
